package com.yonyou.gmmc.service.pointactivity.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yonyou.gmmc.service.pointactivity.dao.ServiceStarMapper;
import com.yonyou.gmmc.service.pointactivity.dao.TtOwnerLotteryMapper;


@Transactional
@Service("ServiceStarService")
public class ServiceStarService {
	
	@Autowired
	private ServiceStarMapper serviceStarMapper;
	
	@Autowired
	private TtOwnerLotteryMapper ttOwnerLotteryMapper;
	
	/**
	 * 新增投票者信息
	 * @param openId
	 */
	public void insertVoter(String openId){
		Map<String, Object> param = new HashMap<>();
		param.put("openId", openId);
		param.put("ifShare", 0);
		param.put("standbyTicket", 5);
		param.put("createDate", new Date());
		serviceStarMapper.insertVoter(param);
	}
	
	/**
	 * 每天重置票数和分享状态
	 */
	public void resetVoter(Map<String, Object> param){
		serviceStarMapper.resetVoter(param);
	}

	/**
	 * 分享后续操做 
	 * @param openId
	 */
	public String doShare(String openId) {
		int ifShare = serviceStarMapper.ifShare(openId);
		if(ifShare == 0){
			Map<String, Object> param = new HashMap<>();
			param.put("openId", openId);
			param.put("updateDate", new Date());
			serviceStarMapper.addTicket(param);
			return "4";
		}else{
			return "3";
		}
	}

	/**
	 * 投票操做
	 * @param openId
	 */
	public String doVote(Map<String, Object> param) {
		String message = "";
		boolean ifVoter = ifVoter(param.get("openId").toString());
		if(!ifVoter){
			message = "2";
			return message;
		}
		int standbyTicket = serviceStarMapper.getStandByTicket(param.get("openId").toString());
		if(standbyTicket == 0){
			message = "1";
			return message;
		}
		int voteType = 0;
		
		message = "HY";
		String vin = ttOwnerLotteryMapper.getVinByOpenId(param.get("openId").toString());
		if(vin != null && !"".equals(vin)){
			int ifVote = serviceStarMapper.queryVoteRecordByVin(vin);
			if(ifVote <= 6){
				voteType = 1;
				message = "CZ";
			}
		}else{
			vin = "";
		}
		param.put("vin", vin);
		param.put("voteType", voteType);
		param.put("createDate", new Date());
		serviceStarMapper.insertVoteRecord(param);
		param.put("updateDate", new Date());
		serviceStarMapper.voteTicket(param);
		message += "T";
		return message;
	}

	public List<Map<String, Object>> getAdvisor(Map<String, Object> param) {
		//判断访客的省份 经销商信息
		if(param.get("openId") == null){
			return serviceStarMapper.getAdvisor(param);
		}else{
			String dealerCode = serviceStarMapper.getDealerCodeByOpenId(param.get("openId").toString());
			if(dealerCode == null || "".equals(dealerCode)){
				return serviceStarMapper.getAdvisor(param);
			}
			else{
				String province = serviceStarMapper.getProvinceByDealerCode(dealerCode);
				String regionCode = serviceStarMapper.getRegionCodeByProvince(dealerCode);
				param.put("dealerCode", dealerCode);
				param.put("province", province);
				param.put("regionCode", regionCode);
				return serviceStarMapper.getAdvisorOrder(param);
			}
		}
	}

	/**
	 * 判断是否可以参与投票
	 * @param openId
	 * @return
	 */
	public boolean ifVoter(String openId) {
		int i = serviceStarMapper.ifVoter(openId);
		if(i == 0){
			return false;
		}
		return true;
	}

	public int getCountAdvisor() {
		return serviceStarMapper.getCountAdvisor();
	}

	public List<Map<String, Object>> queryAdvisor(Map<String, Object> param) {
		return serviceStarMapper.queryAdvisor(param);
	}
}
